﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/*
 * 
 * 发帖   act=newpost  bbstitle=【string】  bbscontent=【string 内容】
 * 回复   act=recallpost  recallcontent=【string 回复内容】 postbbsid=【string 帖子id】
 * 删帖   act=postdel  id=【string 帖子id】
 * 删回复 act=recalldel  id=【string 回复id】
 * 删评论 act=commentdel  id=【string 评论id】
 * 添加评论 act=newcomment  postsrcid=【string 资源id】  commentcontent=【string 评论内容】
 */
public partial class newpost : System.Web.UI.Page
{
    private string CheckUser()
    {
        UserPrivilege UserPri = new UserPrivilege();
        return UserPri.GetSessionName();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       string asaction = Request.QueryString["act"].ToString();
       UserPrivilege UserPri = new UserPrivilege();
       switch(asaction)
       {
           case "newpost":
               {
                   string sessionname = CheckUser();
                   if(sessionname==null)
                   {
                       Response.Write("尚未登录");
                   }
                   else
                   {
                       string title = Request.Form["bbstitle"].ToString().Trim();
                       string content = Request.Form["bbscontent"].ToString().Trim();
                       if (title != "" && content != "")
                       {

                           string istop = "0";
                           if (UserPri.IsBBSmgrPrivilege()==true)
                           {
                               istop = "1";
                           }
                           // DateTime dt=DateTime.Now();
                           MysqlHelp insertpost = new MysqlHelp();
                           string conn = string.Format("insert into bbs (username,title,content,parent,time,top) values('{0}','{1}','{2}','-1','{3}','{4}')", sessionname, title, content, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), istop);
                           int back = insertpost.Runsql(conn);
                           if (back != -1)
                           {
                               Response.Write("帖子发布成功");
                           }
                           else
                           {
                               Response.Write("帖子发布失败");
                           }
                       }
                       else
                       {
                           Response.Write("请填写完整信息");
                       }
                   }
               }
               break;
           case "recallpost":
               {
                   string sessionname = CheckUser();
                   if (sessionname == null)
                   {
                       Response.Write("尚未登录");
                   }
                   else
                   {
                       string postid = Request.Form["postbbsid"].ToString().Trim();
                       string content = Request.Form["recallcontent"].ToString().Trim();
                       if (content != "")
                       {

                           // DateTime dt=DateTime.Now();
                           MysqlHelp insertpost = new MysqlHelp();
                           string conn = string.Format("insert into bbs (username,title,content,parent,time,top) values('{0}','回复','{1}','{2}','{3}','0')", sessionname, content,postid, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                           int back = insertpost.Runsql(conn);
                           if (back != -1)
                           {
                               Response.Write("帖子回复成功");
                           }
                           else
                           {
                               Response.Write("帖子回复失败");
                           }
                       }
                       else
                       {
                           Response.Write("请填写完整信息");
                       }
                   }
               }
               break;
           case "postdel":
               {
                   string delid = Request.QueryString["id"].ToString();
                   MysqlHelp postdelete = new MysqlHelp();
                   string conn1 = string.Format("delete from bbs where id={0}", delid);
                   string conn2 = string.Format("delete from bbs where parent={0}", delid);
                   int back1 = postdelete.Runsql(conn1);
                   if(back1==-1)
                   {
                       Response.Write("删除失败");
                       return;
                   }
                   int back2 = postdelete.Runsql(conn2);
                   Response.Write("删除成功");
               }
               break;
           case "recalldel":
               {
                   string delid = Request.QueryString["id"].ToString();
                   MysqlHelp postdelete = new MysqlHelp();
                   string conn1 = string.Format("delete from bbs where id={0}", delid);
                   int back1 = postdelete.Runsql(conn1);
                   if (back1 != -1)
                   {
                       Response.Write("删除成功");
                   }
                   else
                   {
                       Response.Write("删除失败");
                   }             
               }
               break;
           case "commentdel":
               {
                   string delid = Request.QueryString["id"].ToString();
                   MysqlHelp postdelete = new MysqlHelp();
                   string conn1 = string.Format("delete from srccomment where id={0}", delid);
                   int back1 = postdelete.Runsql(conn1);
                   if (back1 != -1)
                   {
                       Response.Write("删除成功");
                   }
                   else
                   {
                       Response.Write("删除失败");
                   }    
               }
               break;
           case "newcomment":
               {
                   string sessionname = CheckUser();
                   if (sessionname == null)
                   {
                       Response.Write("尚未登录");
                   }
                   else
                   {
                       string postid = Request.Form["postsrcid"].ToString().Trim();
                       string content = Request.Form["commentcontent"].ToString().Trim();
                       if (content != "")
                       {

                           // DateTime dt=DateTime.Now();
                           MysqlHelp insertpost = new MysqlHelp();
                           string conn = string.Format("insert into srccomment (username,content,srcid,time) values('{0}','{1}','{2}','{3}')", sessionname, content, postid, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                           int back = insertpost.Runsql(conn);
                           if (back != -1)
                           {
                               Response.Write("评论成功");
                           }
                           else
                           {
                               Response.Write("评论失败");
                           }
                       }
                       else
                       {
                           Response.Write("请填写完整信息");
                       }
                   }
               }
               break;
       }
    }
}
